package com.rocsun.web.controller;

import cn.hutool.core.util.ObjectUtil;
import com.rocsun.base.RocsunResult;
import com.rocsun.constant.ControllerConstant;
import com.rocsun.service.UserService;
import com.rocsun.user.param.UserRequest;
import com.rocsun.user.result.UserVO;
import com.rocsun.web.utils.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: rocsun-scali
 * @description:
 * @author: yx.guo
 * @create: 2025-11-09 11:58
 **/
@RestController
@RequestMapping(ControllerConstant.AUTH_API)
public class AuthController {

    @Resource
    private UserService userService;

    @Resource
    private JwtUtil jwtUtil;

    @PostMapping("/login")
    public RocsunResult<UserVO> login(@RequestBody UserRequest request, HttpServletResponse response){
        UserVO userVO = userService.login(request);
        // 设置登陆token到response中
        if (ObjectUtil.isNotEmpty(userVO)) {
            String authorization = jwtUtil.generateAuthorization(userVO.getUsername(), userVO.getNickname(), userVO.getRole());
            response.setHeader("Authorization", authorization);
        }

        return RocsunResult.success(userVO);
    }


}
